package cn.com.inossem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.model.ExpenseApplyBean;
import cn.com.inossem.utils.DialogUtil;
import cn.com.inossem.utils.ToastUtil;

/**
 * 添加报销
 * Created by Mickey.Li on 2017-8-8.
 */
public class ProjectAddExpenseActivity extends RequestActivity {
	@BindView(R.id.common_title)
	TextView commonTitle;
	@BindView(R.id.common_right_tv)
	TextView commonRightTv;
	@BindView(R.id.tv_addExpense_sketch)
	TextView tvAddExpenseSketch;
	@BindView(R.id.tv_addExpense_WBS)
	TextView tvAddExpenseWBS;
	@BindView(R.id.tv_addExpense_invoice)
	TextView tvAddExpenseInvoice;
	@BindView(R.id.tv_addExpense_money)
	TextView tvAddExpenseMoney;
	@BindView(R.id.et_addExpense_explain)
	EditText etAddExpenseExplain;
	private ExpenseApplyBean.ExpenseItemBean bean;
	private Intent mIntent;

	@Override
	public int getContentViewId() {
		return R.layout.act_expense_add;
	}

	@Override
	public void initAllMembers(Bundle savedInstanceState) {
		super.initAllMembers(savedInstanceState);
		mIntent = getIntent();
		bean = new ExpenseApplyBean.ExpenseItemBean();
		commonTitle.setText(getString(R.string.expense_add));
		findViewById(R.id.common_right_img).setVisibility(View.GONE);
		commonRightTv.setVisibility(View.VISIBLE);
		commonRightTv.setText(R.string.expense_save);
	}

	@OnClick({R.id.common_back, R.id.common_right_tv, R.id.tv_addExpense_sketch, R.id.tv_addExpense_WBS, R.id.tv_addExpense_invoice, R.id.tv_addExpense_money})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.common_back:
				finish();
				break;
			case R.id.common_right_tv:
				bean.explain = etAddExpenseExplain.getText().toString();
				bean.id = "";
				if (bean.checkData()) {
					mIntent.putExtra("expenseItem", bean);
					setResult(RESULT_OK, mIntent);
					finish();
				} else {
					ToastUtil.show(this, getString(R.string.expense_add_notice));
				}
				break;
			case R.id.tv_addExpense_sketch:
				showDialog(getString(R.string.expense_intro_hint), 1, null, DialogUtil.DialogType.TEXT);
				break;
			case R.id.tv_addExpense_WBS:
				showDialog(getString(R.string.expense_wbs_choice), 2, getIntent().getStringArrayExtra("project_wbs"), DialogUtil.DialogType.CHOICE);
				break;
			case R.id.tv_addExpense_invoice:
				showDialog(getString(R.string.expense_invoice_type_choice), 3, getResources().getStringArray(R.array.addExpense_invoice), DialogUtil.DialogType.CHOICE);
				break;
			case R.id.tv_addExpense_money:
				showDialog(getString(R.string.expense_money_hint), 4, null, DialogUtil.DialogType.NUMBER);
				break;
		}
	}

	private void showDialog(String title, int tag, String[] choices, DialogUtil.DialogType type) {
		DialogUtil.getInstance(this).showDialog(title, tag, choices, type, 0, new DialogUtil.DialogCallBack() {
			@Override
			public void back(int tag, String content, int which) {
				switch (tag) {
					case 1:
						tvAddExpenseSketch.setText(content);
						bean.summary = content;
						break;
					case 2:
						tvAddExpenseWBS.setText(content);
						bean.wbs = content;
						break;
					case 3:
						tvAddExpenseInvoice.setText(content);
						bean.invoiceTyp = "" + which;
						break;
					case 4:
						tvAddExpenseMoney.setText(content);
						bean.amount = Double.parseDouble(content);
						break;
				}
			}
		});
	}
}